* Replication code for: Donnaloja, V. and M. Vink (2023). 
* Like parent, like child: how attitudes towards immigrants spill over to the political inclusion of their children. 
* Journal of Ethnic and Migration Studies.
* Running this .do file from a working directory in which the file "my_data.dta" has been stored with produce 
* the following tables for the paper: Table S1, S2, S3 and S4
* and the following figures: Figure 1 and 2 (main paper) and Figs S3, S4, S5, S6, S7, S8
* note that figures S1, S2 and S9 can be reproduced using the output file "new_data.dta" and R script "7-Interaction terms.R"

clear all

*set working directory*
*cd "[working directory]"


*import spss using "Original.sav", case(lower)
*save "my_data"

use "my_data"

*keep only if Italian citizens
keep if citizenship==1 | citizenship_check==999

*rename vars*
 rename (*attribut11*) (*children_numb*)
 rename (*attribut10*) (*friends_nationality*)
 rename (*attribut1*) (*sex*)
 rename (*attribut2*) (*team*)
 rename (*attribut3*) (*country*)
 rename (*attribut4*) (*residence*)
 rename (*attribut5*) (*legal*)
 rename (*attribut6*) (*occup*)
 rename (*attribut7*) (*education*)
 rename (*attribut8*) (*language*)
 rename (*attribut9*) (*religion*)

**check how many people said yes to all profiles (conjoint* = 3 (both)), and no to all profiles (conjoint* = 4 (neither))
  
  egen outcome_all_yes = anycount(conjoint*), value(3)

*=5 said yes to all
  fre outcome_all_yes
  
  egen outcome_all_no = anycount(conjoint*), value(4)
  
*=5 said no to all  
  fre outcome_all_no
  
*prepare for reshape
*whether first or second in pair 
 rename *_1 *A
 rename *_2 *B
*move prefix of screen number to suffix for reshape
 rename src_1_* *1
 rename src_2_* *2
 rename src_3_* *3
 rename src_4_* *4
 rename src_5_* *5

*now reshape long (1 row per screen)
  reshape long sexA sexB teamA teamB friends_nationalityA friends_nationalityB children_numbA children_numbB countryA countryB residenceA residenceB legalA legalB occupA occupB educationA educationB languageA languageB religionA religionB conjoint, i(caseid) j(screen) string

*reshape long again (1 row per profile)
  rename *A *1
  rename *B *2
  gen id = _n
  reshape long sex team friends_nationality children_numb country residence legal occup education language religion, i(id) j(profile)
  
*rename and recode outcome variable 
  
  replace conjoint = 0 if conjoint==4 //neither get cit
  replace conjoint = 0 if profile==1 & conjoint==2 //only profile 2 gets it and the row is for 1
  replace conjoint = 0 if profile==2 & conjoint==1 //only profile 1 gets it and the row is for 2
  replace conjoint = 1 if conjoint== 3
  replace conjoint = 1 if conjoint==2
  
  rename conjoint outcome  
  label define Outcome  1 "Yes" 0 "No"
  label value outcome Outcome
  
  tab outcome

  
*relabel attributes  
* recode irregular status to make irregular the reference category
  ssc install vreverse
  vreverse legal, gen(legal_status)
  tab legal_status


*relabel religion
  label define Religion 1 Muslim 2 Catholic 3 "No religion"
  label value religion Religion
  tab religion
  
*occupation
   vreverse occup, gen(occupation)
   fre occupation
   label define occupation 1 "Neither works" 2 "Only father works"  3 "Only mother works" 4  "Both work", modify
   label value occupation occupation
   
*residence
   label define residence 1 "Three years" 2 "Five years" 3 "Ten years" 4 "Twenty years"
   label value residence residence
   
*Education 
  label define education 1 "Elementary" 2 "Highschool" 3 "university"
  label value education education
  
*Team support

  recode team (2=1 "Origin only") (1=2 "Italy only") (3=3 "Both"), gen(team_support)
 fre team_support
  
*Friends

  vreverse friends_nationality, gen(friends)
  label define friends 1 "All foreign" 2 "Both" 3 "All Italian", modify
  label value friends friends

*children
 vreverse children_numb, gen(children)



 
**recode respondent characteristics
*age
   recode age_rc (1/2=3 "18-34") (3/4=2 "35-54") (5=1 "55+"), gen(age_new)
	fre age_new
	
	label var age_new "Age"
 
*education
 
  recode education_level_it (1/3=1 "Primary school") (4/6=2 "Highschool") (7/12=3 "University"), gen(educ_new)
  fre educ_new
  label var educ_new "Education"
  
*Voting 2018 elections
  
   recode vote_18_quote_it (1=1 "Five star movement") (2/4=2 "Right-wing party") (5/7=3 "Left-wing party") (8/98=4 "Other"), gen(voting_18)
  fre voting_18
  label var voting_18 "2018 voting"
  
*relabel gender respondent
  label define labels0 1 "Male" 2 "Female", modify

 *relabel region_grouped_it
 label define labels1 1 "North-West" 2 "North-East" 3 "Centre" 4 "South" 5 "Islands", modify

*save data file for R
save new_data.dta, replace

**------------------------------------------------DESCRIPTIVES-----------------------------------------------------------------------------------------------------------------------------------------------------------------
 
   ssc install estout, replace
   which estout  // check versions
   which esttab
   
   
 ***Table S1: balance of attribute levels
 
 ssc install tabout

 tabout sex country religion legal_status residence occupation education language friends team_support children using tableS1.tex, oneway cells(freq col) ///
 style(tex) f(0) font(bold) ptotal(single) replace  
 
 **Table S2: descriptives of respondent characteristics + later add benchmark data to text file for compilation in R
*unweighted 
*tabout gender region_grouped_it age_new educ_new income_hml_it empl_stat_eu voting_18 using descriptives-respondents.txt, replace oneway cells(freq col)

tabout gender region_grouped_it age_new educ_new voting_18 using tableS2.tex,  replace oneway cells(freq col) f(0) font(bold) ptotal(single) style(tex) 
 
 *install new conjoint package 
  *ssc install conjoint

  *analysis without weights
*start analysis of AMCEs

 *conjoint outcome sex country religion legal_status residence occupation education language friends team_support children,  est(amce) id(caseid) constraint(country#legal_status) base(1 1 1 2 1 4 1 1 3 2 3) graph
  
*marginal means
  *conjoint outcome sex country religion legal_status residence occupation education language friends team_support children,  est(mm) id(caseid) graph
  
*average acceptance rate

tab outcome

*average acceptance rate excluding irregular profiles

tab outcome if legal_status==2 

*Table S3: Average acceptance rate per group of respondents (descriptives)
 
tabout age_new educ_new voting_18 outcome using tableS3.tex, font(bold) ptotal(single) style(tex) append cells(row)

* run other do-files to produce rest of the tables and figures
do "2-AMCEs.do"
do "3-Figure1.do"
do "4-TableS4.do"
do "5-Robustness.do"
do "6-MMs by respondent groups.do"


* end
